<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>时间差</title>
</head>

<body>

    <div id="timeBox"></div>
    <script>
        //注意：月份是从0开始的，所以这里的2就是3月
        var time2 = new Date(2020, 2, 11, 21, 00, 00)

        function subTime(t1, t2) {
            var str = ''
            var t = parseInt(Math.abs(t1.getTime() - t2.getTime()) / 1000)
            var day = parseInt(t / (60 * 60 * 24))
            var subDay = t - day * 60 * 60 * 24
            var hours = parseInt(subDay / (60 * 60))
            var subMinute = subDay - hours * 60 * 60
            var minutes = parseInt(subMinute / 60)
            var seconds = t % 60

            str += day + "天 "
            str += hours + "小时 "
            str += minutes + "分钟 "
            str += seconds + "秒"

            return str
        }

        setInterval(function () {
            var time1 = new Date()
            var res = subTime(time1, time2)
            console.log(res);
            timeBox.innerText = res
        }, 1000)
    </script>
</body>

</html>